<!--
 * @Author: Haochen
 * @Date: 2021-12-08 20:58:52
 * @LastEditTime: 2021-12-09 19:48:44
 * @FilePath: \students-system\src\views\teachers\DeleteTeacher.vue
-->
<template>
  <div>
    <h4>删除教师</h4>

    <select v-model="teachersId">
      <option :value="undefined" disabled>请选择教师</option>
      <option
        v-for="teacher in teachers"
        :key="teacher._id"
        :value="teacher._id"
      >
        {{ teacher.name }}
      </option>
    </select>
    &nbsp;
    <button @click="deleteTeacher()">确认删除</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      teachersId: undefined,
      teachers: [],
    };
  },
  created() {
    this.getAllTeachers();
  },
  methods: {
    async getAllTeachers() {
      const { data: res } = await this.$api.teachers.getAllTeachers();
      this.teachers = res.data.rows;
    },

    async deleteTeacher() {
      const { data: res } = await this.$api.teachers.deleteTeacher({
        _id: this.teachersId,
      });

      if (res.code) {
        alert(res.message);
        this.teachersId = undefined;
        this.getAllTeachers();
      } else {
        alert(res.message);
      }
    },
  },
};
</script>

<style></style>
